package com.pad.producter.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.pad.producter.domain.OperLog;
import com.pad.producter.query.OperLogQuery;
import com.pad.producter.result.JSONResult;
import com.pad.producter.result.PageList;
import com.pad.producter.service.IOperLogService;

import lombok.extern.slf4j.Slf4j;

/**
 * @desc
 * @author yangdechao
 * @since 2021-12-21
 */
@RestController
@RequestMapping("/operLog")
@Slf4j
public class OperLogController {

    @Autowired
    public IOperLogService operLogService;

    /**
     * 保存和修改操作公用此方法
     * @param operLog 前端传递来的实体数据
     */
    @PostMapping(value="/save")
    public JSONResult save(@RequestBody OperLog operLog){
    	log.info("------------开始执行save方法----------------");
        if(operLog.getId()!=null){
            operLogService.updateById(operLog);
        }else{
            operLogService.insert(operLog);
        }
        log.info("-----------save方法执行完毕----------------");
        return JSONResult.success();
    }
    
    @PostMapping(value="/save2")
    public JSONResult save2(@RequestBody OperLog operLog){
    	log.info("------------开始执行save方法----------------");
        if(operLog.getId()!=null){
            operLogService.updateById(operLog);
        }else{
            operLogService.insert(operLog);
        }
        log.info("-----------save方法执行完毕----------------");
        return JSONResult.success();
    }

    /**
    * 根据ID删除指定对象信息
    * @param id
    */
    @DeleteMapping(value="/{id}")
    public JSONResult delete(@PathVariable("id") Long id){
            operLogService.deleteById(id);
        return JSONResult.success();
    }

    //根据ID查询对象详情信息
    @GetMapping(value = "/{id}")
    public JSONResult get(@PathVariable("id")Long id)
    {
        return JSONResult.success(operLogService.selectById(id));
    }


    /**
    * 查看所有对象数据（不分页）
    */
    @GetMapping(value = "/list")
    public JSONResult list(){
        return JSONResult.success(operLogService.selectList(null));
    }


    /**
    * 分页查询数据
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping(value = "/pagelist")
    public JSONResult pageList(@RequestBody OperLogQuery query)
    {
        Page<OperLog> page = new Page<OperLog>(query.getPage(),query.getRows());
        page = operLogService.selectPage(page);
        return JSONResult.success(new PageList<OperLog>(page.getTotal(), page.getRecords()));
    }
}